package com.suredy.app.porject.model.vo;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.format.annotation.DateTimeFormat;

import com.suredy.app.porject.constants.WorkPlanMilestone;
import com.suredy.security.entity.UserEntity;

/**
 * 工期计划筛选模型
 * 
 * @author VIVID.G
 * @since 2016-2-24
 * @version v0.1
 */
public class WorkPlanFilterModel {

	/* 合同编号 */
	private String contractNo;

	/* 合同名称 */
	private String contractName;

	/* 里程碑 */
	private String planMilestone;

	/* 开始时间 */
	@DateTimeFormat(pattern = "yyyy-MM-dd")
	private Date planMinStartDatePoint;

	/* 开始时间 */
	@DateTimeFormat(pattern = "yyyy-MM-dd")
	private Date planMaxStartDatePoint;

	/* 结束时间 */
	@DateTimeFormat(pattern = "yyyy-MM-dd")
	private Date planMinDatePoint;

	/* 结束时间 */
	@DateTimeFormat(pattern = "yyyy-MM-dd")
	private Date planMaxDatePoint;

	/* 负责人 */
	private String planHeader;

	/* 计划变更 */
	private Boolean planChanged;

	/* 执行情况 */
	private Boolean planCompleted;

	/* 创建人 */
	private String planCreator;

	public String getContractNo() {
		return contractNo;
	}

	public void setContractNo(String contractNo) {
		this.contractNo = contractNo;
	}

	public String getContractName() {
		return contractName;
	}

	public void setContractName(String contractName) {
		this.contractName = contractName;
	}

	public String getPlanMilestone() {
		return planMilestone;
	}

	public void setPlanMilestone(String planMilestone) {
		this.planMilestone = planMilestone;
	}

	public Date getPlanMinStartDatePoint() {
		return planMinStartDatePoint;
	}

	public void setPlanMinStartDatePoint(Date planMinStartDatePoint) {
		this.planMinStartDatePoint = planMinStartDatePoint;
	}

	public Date getPlanMaxStartDatePoint() {
		return planMaxStartDatePoint;
	}

	public void setPlanMaxStartDatePoint(Date planMaxStartDatePoint) {
		this.planMaxStartDatePoint = planMaxStartDatePoint;
	}

	public Date getPlanMinDatePoint() {
		return planMinDatePoint;
	}

	public void setPlanMinDatePoint(Date planMinDatePoint) {
		this.planMinDatePoint = planMinDatePoint;
	}

	public Date getPlanMaxDatePoint() {
		return planMaxDatePoint;
	}

	public void setPlanMaxDatePoint(Date planMaxDatePoint) {
		this.planMaxDatePoint = planMaxDatePoint;
	}

	public String getPlanHeader() {
		return planHeader;
	}

	public void setPlanHeader(String planHeader) {
		this.planHeader = planHeader;
	}

	public Boolean getPlanChanged() {
		return planChanged;
	}

	public void setPlanChanged(Boolean planChanged) {
		this.planChanged = planChanged;
	}

	public Boolean getPlanCompleted() {
		return planCompleted;
	}

	public void setPlanCompleted(Boolean planCompleted) {
		this.planCompleted = planCompleted;
	}

	public String getPlanCreator() {
		return planCreator;
	}

	public void setPlanCreator(String planCreator) {
		this.planCreator = planCreator;
	}

	public List<Object> makeQl() {
		String ql = "SELECT T FROM ContractWorkPlan T LEFT JOIN T.contract C WHERE 1=1";

		return this.makeWhereQl(ql, true);
	}

	public List<Object> makeCountQl() {
		String ql = "SELECT COUNT(T) FROM ContractWorkPlan T LEFT JOIN T.contract C WHERE 1=1";

		return this.makeWhereQl(ql, false);
	}

	private List<Object> makeWhereQl(String ql, boolean order) {
		List<Object> ret = new ArrayList<Object>();

		List<Object> params = new ArrayList<Object>();

		// 合同编号
		if (!StringUtils.isBlank(this.contractNo)) {
			ql += " AND C.no like ?";
			params.add("%" + this.contractNo + "%");
		}

		// 合同名称
		if (!StringUtils.isBlank(this.contractName)) {
			ql += " AND C.name like ?";
			params.add("%" + this.contractName + "%");
		}

		// 里程碑
		if (this.planMilestone != null) {
			WorkPlanMilestone m = WorkPlanMilestone.getByCode(this.planMilestone);
			if (m != null) {
				ql += " AND T.milestone = ?";
				params.add(m);
			}
		}

		// 开始时间
		if (this.planMinStartDatePoint != null) {
			ql += " AND T.startDatePoint >= ?";
			params.add(this.planMinStartDatePoint);
		}

		// 开始时间
		if (this.planMaxStartDatePoint != null) {
			ql += " AND T.startDatePoint <= ?";
			params.add(this.planMaxStartDatePoint);
		}

		// 结束时间
		if (this.planMinDatePoint != null) {
			ql += " AND T.datePoint >= ?";
			params.add(this.planMinDatePoint);
		}

		// 结束时间
		if (this.planMaxDatePoint != null) {
			ql += " AND T.datePoint <= ?";
			params.add(this.planMaxDatePoint);
		}

		// 负责人
		if (!StringUtils.isBlank(this.planHeader)) {
			ql += " AND T.header like ?";
			params.add("%" + this.planHeader + "%");
		}

		// 计划变更
		if (this.planChanged != null) {
			// 已变更
			if (this.planChanged)
				ql += " AND T.changed = ?";

			// 未变更
			else
				ql += " AND (T.changed IS NULL OR T.changed = ?)";

			params.add(this.planChanged);
		}

		// 执行情况
		if (this.planCompleted != null) {
			// 已付款
			if (this.planCompleted)
				ql += " AND T.completed = ?";

			// 未付款
			else
				ql += " AND (T.completed IS NULL OR T.completed = ?)";

			params.add(this.planCompleted);
		}

		// 创建人
		if (!StringUtils.isBlank(this.planCreator)) {
			ql += " AND T.creater = ?";
			params.add(new UserEntity(this.planCreator));
		}

		// 排序
		if (order)
			ql += " ORDER BY T.completed, T.startDatePoint, T.datePoint";

		ret.add(ql);
		ret.add(params);

		return ret;
	}
}
